package com.cg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cg.dto.AddLikeDTO;
import com.cg.dto.AddQuestionDTO;
import com.cg.dto.UpdateQuestionDTO;
import com.cg.entity.Question;
import com.cg.entity.UserLike;
import com.cg.mapper.QuestionMapper;
import com.cg.result.R;
import com.cg.service.AccountService;
import com.cg.service.ILikeService;
import com.cg.service.IQuestionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 加
 * @since 2025-01-17
 */
@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper, Question> implements IQuestionService {
    @Autowired
    private IQuestionService questionService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private ILikeService likeService;

    @Override
    public R addQuestion(AddQuestionDTO addQuestionDTO) {
        Question question=new Question();
        BeanUtils.copyProperties(addQuestionDTO,question);
        save(question);
        return R.Success("新增成功");
    }

    @Override
    public R<List<Question>> showQuestion() {
        LambdaQueryWrapper<Question> queryWrapper=new LambdaQueryWrapper<>();
        List<Question> questionList=list(queryWrapper);
        if(questionList!=null) {
            return R.Success(questionList);
        }
        return R.Failed("未找到");
    }

    @Override
    public R getQuestionDetail(Long questionId) {
        Question question=getById(questionId);
        return R.Success(question);
    }

    @Override
    public R updateQuestion(UpdateQuestionDTO updateQuestionDTO) {
        Question question=getById(updateQuestionDTO.getQuestionId());
        BeanUtils.copyProperties(updateQuestionDTO,question);
        return R.Success(question);
    }

    @Override
    public R deleteQuestion(Long questionId) {
        if(questionId!=null&&selectQuestionById(questionId).equals("1")) {
            removeById(questionId);
            return R.Success("删除成功");
        }else{
            return R.Failed("不存在该题目，不可删除");
        }
    }

    @Override
    public String selectQuestionById(Long questionId) {
        Question question=getById(questionId);
        if (question!=null){
            return "1";
        }
        return "0";
    }

    @Override
    public Question selectQuestionById2(Long questionId) {
        Question question=getById(questionId);
        return question;
    }


    @Override
    public R addLike(AddLikeDTO addLikeDTO) {
        UserLike like=new UserLike();
        like.setQuestionId(addLikeDTO.getQuestionId());
        like.setUserId(accountService.getCurrentUserId());
        like.setContent(questionService.selectContentById(addLikeDTO.getQuestionId()));
        likeService.save(like);
        return R.Success("收藏问题成功");
    }

    @Override
    public String selectContentById(Long questionId) {
        Question question=getById(questionId);
        return question.getContent();
    }
}
